const Layout = 'Layout';

const menu = [
  {
    path: '/application',
    name: 'application',
    icon: 'Menu',
    component: Layout,
    redirect: '/application/app-list',
    meta: {
      title: '应用',
      description: '这是应用模块，好用的应用都在这里',
    },
    children: [
      {
        path: 'app-list',
        name: 'appList',
        icon: 'Help',
        component: 'src/views/application/index.vue',
        meta: {
          title: '应用列表',
        },
      },
      {
        path: 'api-doc',
        name: 'apiDoc',
        icon: 'Document',
        component: 'src/views/application/apiDoc/index.vue',
        meta: {
          title: '接口文档',
        },
      },
      {
        path: 'open-ai',
        name: 'openAi',
        icon: 'Coordinate',
        component: 'src/views/application/openAi/index.vue',
        meta: {
          title: 'Ai助手',
          fullScreen: true,
          newPage: '/application/open-ai',
        },
      },
      {
        path: 'ai-painter',
        name: 'aiPainter',
        icon: 'PictureRounded',
        component: 'src/views/application/aiPainter/index.vue',
        meta: {
          title: 'Ai画师',
        },
      },
      {
        path: 'network-disk',
        name: 'networkDisk',
        icon: 'UploadFilled',
        component: 'src/views/application/networkDisk/index.vue',
        meta: {
          title: '网盘',
        },
      },
      {
        path: 'comapp/:page*',
        name: 'comapp',
        icon: 'Cherry',
        component: 'src/views/application/comapp/index.vue',
        meta: {
          defaultPath: '/application/comapp/#/', // 自定义地址栏与跳转地址
          title: '组件',
        },
      },
    ],
  },
  {
    path: '/manage',
    name: 'manage',
    icon: 'Setting',
    component: Layout,
    redirect: '/manage/blog',
    meta: {
      title: '管理',
      description: '这是管理模块，可以对多项功能进行管理',
    },
    children: [
      {
        path: 'conversation',
        name: 'conversation',
        icon: 'ChatLineRound',
        component: 'src/views/manage/conversation/index.vue',
        meta: {
          title: '对话管理',
        },
      },
      {
        path: 'blog',
        name: 'blog',
        icon: 'Notebook',
        component: 'src/views/manage/blog/index.vue',
        meta: {
          title: '博客管理',
        },
      },
      {
        path: 'user',
        name: 'user',
        icon: 'User',
        component: 'src/views/manage/user/index.vue',
        meta: {
          title: '用户管理',
        },
      },
      {
        path: 'role',
        name: 'role',
        icon: 'OfficeBuilding',
        component: 'src/views/manage/role/index.vue',
        meta: {
          title: '角色管理',
        },
      },
      {
        path: 'menu',
        name: 'menu',
        icon: 'Operation',
        component: 'src/views/manage/menu/index.vue',
        meta: {
          title: '菜单管理',
        },
      },
    ],
  },
];

interface MenuItem {
  path: string;
  name: string;
  icon: string;
  component: any;
  redirect?: string;
  meta: {
    title: string;
    description?: string;
    fullScreen?: boolean;
    newPage?: string;
    defaultPath?: string;
  };
  children?: MenuItem[];
  id?: string;
  parentId?: string;
  isLeaf?: boolean;
}

function flattenMenu(
  menu: MenuItem[],
  parentItem: MenuItem = {
    path: '',
    name: '',
    icon: '',
    component: '',
    meta: {
      title: '',
    },
    children: [],
  },
): MenuItem[] {
  parentItem.id = parentItem.id || '00';
  return menu.reduce<MenuItem[]>((acc, item, index) => {
    const id = parentItem.id + index.toString().padStart(2, '0');
    const newItem: MenuItem = {
      ...item,
      id,
      path: parentItem.path + '/' + item.path,
      parentId: parentItem.id,
    };
    newItem.path = newItem.path.replace(/\/+/g, '/');
    newItem.path = newItem.path.replace(/\/+$/, '');

    acc.push(newItem);

    if (newItem.children) {
      newItem.isLeaf = false;
      acc.push(...flattenMenu(newItem.children, newItem));
      delete newItem.children;
    } else {
      newItem.isLeaf = true;
    }

    return acc;
  }, []);
}

export const flatMenu = flattenMenu(menu);
